﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using System.IO;

namespace Pongster
{
    public struct Settings
    {
        public static int screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
        public static int screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
        public static int screenIndex = 0;
        
        public const int basescreenWidth = 1920;
        public const int basescreenHeight = 1080;

        public static bool screenMode = false;
        public static int screenModeIndex = 0;
        
        public static Vector2 aspectRatio = Vector2.Zero;
        public static Rectangle area = new Rectangle(0, 0, screenWidth, screenHeight);

        public static float musicVolume = 0.3f;
        public static float soundVolume = 0.4f;
        public static bool mediaPlayerFree = true;

        public static int yOffset = 0;
        public static int xOffset = 0;

        //Helpers
        public static int resWidth = screenWidth;
        public static int resHeight = screenHeight;
        public static int resIndex = screenIndex;
        public static bool resMode = screenMode;
        public static int resModeIndex = screenIndex;

        public static bool isCursorVisible = true;

        public static void Init(int width, int height, int index, bool mode, int modeindex, int mvolume, int svolume)
        {
            screenWidth = width;
            screenHeight = height;
            screenIndex = index;
            screenMode = mode;
            screenModeIndex = modeindex;
            musicVolume = (float)(mvolume / 10f);
            soundVolume = (float)(svolume / 10f);

            resWidth = screenWidth;
            resHeight = screenHeight;
            resMode = screenMode;
            resModeIndex = screenModeIndex;
            resIndex = screenIndex;
        }


        public static void Update(GameTime gameTime)
        {
            soundVolume = MathHelper.Clamp(soundVolume, 0f, 1f);
            musicVolume = MathHelper.Clamp(musicVolume, 0f, 1f);

            if (mediaPlayerFree == true)
            {
                MediaPlayer.Volume = musicVolume;
            }
            SoundEffect.MasterVolume = soundVolume;
            
        }

        public static void SaveSettingsFile()
        {
                string[] settingsInformation = new string[7];
                settingsInformation[0] = Convert.ToString(Settings.screenWidth);
                settingsInformation[1] = Convert.ToString(Settings.screenHeight);
                settingsInformation[2] = Convert.ToString(Settings.screenIndex);
                settingsInformation[3] = Convert.ToString(Settings.screenMode);
                settingsInformation[4] = Convert.ToString(Settings.screenModeIndex);
                settingsInformation[5] = Convert.ToString(Settings.musicVolume * 10);
                settingsInformation[6] = Convert.ToString(Settings.soundVolume * 10);
                File.WriteAllLines("settings.data", settingsInformation);            
        }


        public static void applyAspectRatio()
        {
            if ((float)screenWidth / (float)screenHeight < 1.7f && (float)screenWidth / (float)screenHeight > 1f)
            {
                float aspectratio = ((float)screenWidth / (float)basescreenWidth);
                float width = (float)basescreenWidth * aspectratio;
                float height = (float)basescreenHeight * aspectratio;
                yOffset = (int)(((float)screenHeight - height) / 2f);
                area = new Rectangle(0, yOffset, (int)width, (int)height);
                xOffset = 0;
                return;
            }
            else if ((float)screenWidth / (float)screenHeight < 1f)
            {
                float aspectratio = ((float)screenHeight / (float)basescreenHeight);
                float width = (float)basescreenWidth * aspectratio;
                float height = (float)basescreenHeight * aspectratio;
                xOffset = (int)(((float)screenWidth - width) / 2f);
                area = new Rectangle(xOffset, 0, (int)width, (int)height);
                yOffset = 0;
                return;
            }
            else
            {
                area = new Rectangle(0, 0, screenWidth, screenHeight);
                yOffset = 0;
                xOffset = 0;
            }
        }

        public static void resetResValues()
        {
            resWidth = screenWidth;
            resHeight = screenHeight;
            resMode = screenMode;
            resModeIndex = screenModeIndex;
            resIndex = screenIndex;
        }


    }
}
